package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAgency;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAgencyRepository;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.query.pageparam.AgencyPageParam;
import com.wgchao.identityrightsmanagement.query.vo.AgencyVo;
import com.wgchao.identityrightsmanagement.query.vo.AgencyVoConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @作者：wgchao
 * @时间：20:33 2020/6/14 0014
 * @用途：
 **/
@Service
public class AgencyQuery {
    @Autowired
    private SysBaseAgencyRepository sysBaseAgencyRepository;
    @Autowired
    private AgencyVoConvert agencyVoConvert;

    /**
     * 查询机构信息
     * @param agencyCode
     * @return
     */
    public AgencyVo findByCode(String agencyCode){
        SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCodeAndDelFalse(agencyCode);
        if(sysBaseAgency != null){
            return agencyVoConvert.convert(sysBaseAgency);
        }
        return null;
    }

    /**
     * 条件分页查询
     * @param agencyPageParam
     * @return
     */
    public PageData<AgencyVo> findByPage(AgencyPageParam agencyPageParam){
        Specification<SysBaseAgency> specification = new Specification<SysBaseAgency>() {
            @Override
            public Predicate toPredicate(Root<SysBaseAgency> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(agencyPageParam.getAgencyCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("agencyCode"), agencyPageParam.getAgencyCode());
                    predicates.add(predicate);
                }
                if(!StringUtils.isEmpty(agencyPageParam.getName())){
                    Predicate predicate = criteriaBuilder.like(root.get("agencyName"), "%"+agencyPageParam.getName()+"%");
                    predicates.add(predicate);
                }
                if(agencyPageParam.getDisabled() != null){
                    if(agencyPageParam.getDisabled()){
                        Predicate predicate = criteriaBuilder.isTrue(root.get("disabled"));
                        predicates.add(predicate);
                    }else{
                        Predicate predicate = criteriaBuilder.isFalse(root.get("disabled"));
                        predicates.add(predicate);
                    }
                }
                Predicate predicate = criteriaBuilder.isFalse(root.get("del"));
                predicates.add(predicate);

                Predicate[] predicatesArry = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicatesArry));
            }
        };

        Pageable pageable = PageRequest.of(agencyPageParam.getCurr()-1, agencyPageParam.getLimit(), Sort.by(Sort.Order.desc("createTime")));

        Page<SysBaseAgency> sysBaseAgencyPage = sysBaseAgencyRepository.findAll(specification, pageable);

        if(sysBaseAgencyPage.isEmpty()){
            return new PageData<AgencyVo>(0L, agencyPageParam.getCurr(), null);
        }
        List<AgencyVo> agencyVoList = sysBaseAgencyPage.get().map(sysBaseAgency -> {
            return agencyVoConvert.convert(sysBaseAgency);
        }).collect(Collectors.toList());

        return new PageData<AgencyVo>(sysBaseAgencyPage.getTotalElements(), agencyPageParam.getCurr(), agencyVoList);
    }
}
